<?php
/**
 * 站点管理
 * @author zhu
 *
 */
namespace App\CLI;

use Forxx\console\CLIRunner;
use Forxx\console\CConsole;

class CSite  extends CLIRunner
{
    private static $_site_list_file = "site_lists.txt";

	public function make_action($site_name=null)
    {
        if(empty($site_name))
        {
            $site_name = CConsole::prompt("input the site name","www.okmnb.com");
        }
        if(is_dir(self::get_base_dir().$site_name))
        {
            $is_over = CConsole::confirm("site {$site_name} Already Exists! over write it ? ");
            if(!$is_over)
            {
                 return;
            }            
        }
        self::add_site($site_name);
        self::make_dir($site_name);
        self::make_file($site_name);
        CConsole::print_line("make site {$site_name} completed!");
    }

    public function do_all_site_action()
    {
        $sites = self::get_site_lists();
        $file_tpl_list = self::get_tpl_file(true);
        $opera_list = ['replace','delete','add'];
        $do = CConsole::choice($opera_list);
        while(($filename = CConsole::prompt("input the file name to do {$opera_list[$do]} ","")) !== "")
        {
            if((!isset($file_tpl_list[$filename])) && $do != 1)
            {
                CConsole::print_line("invalid file name `{$filename}`");
                continue;
            }
            foreach($sites as $site)
            {
                $path = self::get_base_dir().$site.DIRECTORY_SEPARATOR.$filename;
                //CConsole::print_line("{$opera_list[$do]} file : '{$path}'");
                switch($do)
                {
                    case 0:
                        file_put_contents($path,file_get_contents($file_tpl_list[$filename]));
                        break;
                    case 1:
                        @unlink($path);
                        break;
                    case 2:
                        if(!file_exists($path))
                        {
                            file_put_contents($path,file_get_contents($file_tpl_list[$filename]));
                        }
                    break;
                }
            }


        }
    }

    protected static function get_base_dir(){
        static $base_dir = "";
        if(empty($base_dir))
        {
            $base_dir = CConsole::prompt("input the site name",L_WORKSPACE_PATH);
        }
        return $base_dir;
    }

    protected static function get_site_lists()
    {
        $content = file_get_contents(L_APP_PATH.'CLI'.DIRECTORY_SEPARATOR.self::$_site_list_file);
        $sites = explode("\n",$content);
        $sites = array_map("trim",$sites);
        return array_filter($sites);
    }

    protected static function add_site($site_name)
    {
        file_put_contents(L_APP_PATH.'CLI'.DIRECTORY_SEPARATOR.self::$_site_list_file,$site_name."\n",FILE_APPEND);
    }

    protected static function make_dir($site_name)
    {
     	$paths=self::get_all_path($site_name);
        foreach ($paths as $path)
        {
        	\Forxx\FileHelper::create_folder($path,0755);
        	CConsole::print_line("make directory '{path}' \t ok!",['{path}'=>$path]);
        }
    }
    
    protected static function make_file($site_name)
    {
        $need_all = true;
    	$files=self::get_tpl_file($need_all);
    	foreach ($files as $to => $from)
    	{    
    		$file = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.$to;
    		if(file_exists($file))
    		{
    			continue;
    		}
    		$content = file_get_contents($from);
    		file_put_contents($file, $content);
    		CConsole::print_line("make file '{to}' \t ok!",['{to}'=>$to]);
    	}
    }
    
    protected static function get_all_path($site_name)
    {
    	$res = [];
    	$res[] = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.'wwwroot'.DIRECTORY_SEPARATOR.'css';    	
    	$res[] = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.'wwwroot'.DIRECTORY_SEPARATOR.'js';
    	$res[] = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.'view'.DIRECTORY_SEPARATOR.'common';
    	$res[] = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.'view'.DIRECTORY_SEPARATOR.'main';
    	$res[] = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.'view'.DIRECTORY_SEPARATOR.'page';
    	$res[] = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.'Action';
		$res[] = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.'Action'.DIRECTORY_SEPARATOR.'Page';
		$res[] = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.'Action'.DIRECTORY_SEPARATOR.'Service';
    	$res[] = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.'Api';
    	$res[] = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.'CLI';
    	$res[] = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.'conf';
    	$res[] = self::get_base_dir().$site_name.DIRECTORY_SEPARATOR.'Dao';
    	return $res;
    }
        
    protected static function get_tpl_file($need_all = false) {
		$res = [ 
				// index page
				'wwwroot' . DIRECTORY_SEPARATOR . 'index.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'index.php',
				// console enterpoint
				'command' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'command',
                'command.bat' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'command.bat',
				
				// console CRoute
				'CLI' . DIRECTORY_SEPARATOR . 'CRoute.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'CRoute.php',
				
				// console CGenerate
				'CLI' . DIRECTORY_SEPARATOR . 'CGenerate.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'CGenerate.php',

				// console CCache
				'CLI' . DIRECTORY_SEPARATOR . 'CCache.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'CCache.php',
				
				// error page
				'view' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'error.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'error.php',
				
				// web_options conf
				'conf' . DIRECTORY_SEPARATOR . 'web_options.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'config.web_options.php',
				'conf' . DIRECTORY_SEPARATOR . 'route_rules.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'config.route_rules.php',
							
				
				// const page
				'conf' . DIRECTORY_SEPARATOR . 'const.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'config.const.php',
				// redis cache config
				'conf' . DIRECTORY_SEPARATOR . 'cache.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'config.cache.php',

				// database conf
				'conf' . DIRECTORY_SEPARATOR . 'db.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'config.db.php',
				// id_maker conf
				'conf' . DIRECTORY_SEPARATOR . 'id_maker.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'config.id_maker.php',
		];

		$test = [
				//action
				'Action' .DIRECTORY_SEPARATOR.'Page'. DIRECTORY_SEPARATOR . 'test_page.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'Action.test_page.php',
				'Action' .DIRECTORY_SEPARATOR.'Service'. DIRECTORY_SEPARATOR . 'test_service.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'Action.test_service.php',
				
				//api 
		        'Api' . DIRECTORY_SEPARATOR . 'Test.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'Api.Test.php',
		        
		        //dao
		        'Dao' . DIRECTORY_SEPARATOR . 'TestTable.php' => '.' . DIRECTORY_SEPARATOR . 'TPL' . DIRECTORY_SEPARATOR . 'Dao.TestTable.php',
			
		];

		if($need_all)
		{
			$res = array_merge($res,$test);
		}
		
		return $res;
	}    

}